﻿using Moq;
using NUnit.Framework;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using TabbedTemplate.Models;
using TabbedTemplate.Services;
using TabbedTemplate.ViewModels;

namespace UnitTest.ViewModels
{
    public class TestQueryResultPageViewModel
    {
        [Test]
        public async Task TestPageAppearingCommandWithResult()
        {
            var Query = new ClassifySearchParameter
            {
                Tag = Tag.ChineseRestuarant.CnName,
                Input = "饭"
            };
            var queryServiceMock = new Mock<IQueryService>();
            var queryService = queryServiceMock.Object;
            var storeService = new StoreService(new Mock<IPreferenceStorage>().Object,
                new LocationService(new GeoLocationService()));
            var preferenceStorageMock = new Mock<IPreferenceStorage>();
            var preferenceStorage = preferenceStorageMock.Object;
            var queryResultPageViewModel =
                new QueryResultPageViewModel(storeService, preferenceStorage, queryService, null, null);
            queryResultPageViewModel.Query = Query;
            queryResultPageViewModel.queryName = "";
            var statusList = new List<string>();
            queryResultPageViewModel.PropertyChanged += (sender, args) =>
            {
                if (args.PropertyName == nameof(TabbedTemplate.ViewModels.QueryResultPageViewModel.Status))
                {
                    statusList.Add(queryResultPageViewModel.Status);
                }
            };
            var cityName = "沈阳市";
            preferenceStorageMock.Setup(p => p.Get("City", "北京市")).Returns(cityName);
            var res = new SearchItemToReturn
            {
                HasCache = false,
                SearchItem = new SearchItemCache()
            };
            queryServiceMock.Setup(p => p.GetSearchItemByString(Query.Input,
                cityName)).ReturnsAsync(res);
            await queryResultPageViewModel.PageAppearingCommandFunction();
            Assert.AreEqual(statusList[1], "");
        }

        [Test]
        public async Task TestPageAppearingCommandWithoutResult()
        {
            var Query = new ClassifySearchParameter
            {
                Tag = "",
                Input = "/"
            };
            var queryServiceMock = new Mock<IQueryService>();
            var queryService = queryServiceMock.Object;
            var storeServiceMock = new Mock<IStoreService>();
            var storeService = storeServiceMock.Object;
            var preferenceStorageMock = new Mock<IPreferenceStorage>();
            var preferenceStorage = preferenceStorageMock.Object;
            var queryResultPageViewModel =
                new QueryResultPageViewModel(storeService, preferenceStorage, queryService, null, null);
            queryResultPageViewModel.Query = Query;
            queryResultPageViewModel.queryName = "";
            var statusList = new List<string>();
            queryResultPageViewModel.PropertyChanged += (sender, args) =>
            {
                if (args.PropertyName == nameof(TabbedTemplate.ViewModels.QueryResultPageViewModel.Status))
                {
                    statusList.Add(queryResultPageViewModel.Status);
                }
            };
            var cityName = "沈阳市";
            preferenceStorageMock.Setup(p => p.Get("City", "北京市")).Returns(cityName);
            var res = new SearchItemToReturn
            {
                HasCache = false,
                SearchItem = new SearchItemCache()
            };
            var c = new List<Store>();
            queryServiceMock.Setup(p => p.GetSearchItemByString(Query.Input,
                cityName)).ReturnsAsync(res);
            storeServiceMock.Setup(p => p.SearchStoresByRegionFromWebAsync(
                "美食;" + Query.Input, cityName,
                It.IsAny<int>())).ReturnsAsync(c);

            await queryResultPageViewModel.PageAppearingCommandFunction();

            Assert.AreEqual(statusList[2], QueryResultPageViewModel.NoResult);
        }

        [Test]
        public async Task TestPoetryTappedCommand()
        {
            var punchNavigationServiceMock =
                new Mock<IPunchNavigationService>();
            var mockPunchNavigationService = punchNavigationServiceMock.Object;
            var queryResultPageViewModel =
                new QueryResultPageViewModel(null, null, null, null, mockPunchNavigationService);
            var store = new Store();

            await queryResultPageViewModel.PoetryTappedCommandFunction(store);
            punchNavigationServiceMock.Verify(p => p.NavigateToAsync(
                ContentNavigationConstant.StorePunchesPage, store), Times.Once);

        }
    }
}
